package com.pearlsoft.rms;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.pearlsoft.rms.vcenter.History;
import com.pearlsoft.rms.vcenter.Realtime;

/**
 * 
 * @author rainshow
 * @Created 2012-5-10 上午10:46:27
 */

@Service("vcenterMonitor")
@Scope("singleton")
public class VcenterMonitorImpl implements VcenterMonitor {
    private History history;
    private Realtime realtime;
    private static Log logger = LogFactory.getLog(VcenterMonitorImpl.class);

    public MetricResult getHistoryCpuInfo(String vmname, String counterName, int starttime, int duration,
            int interval) {
        try {
            return history.getHistory(vmname, "cpu", counterName, starttime, duration, interval);
        } catch (Throwable t) {
            logger.error(t);
            return new MetricResult();
        }
    }

    @Override
    public MetricResult getHistoryDiskInfo(String vmname, String counterName, int starttime, int duration,
            int interval) {
        try {
            return history.getHistory(vmname, "disk", counterName, starttime, duration, interval);
        } catch (Throwable t) {
            logger.error(t);
            return new MetricResult();
        }
    }

    @Override
    public MetricResult getHistoryMemInfo(String vmname, String counterName, int starttime, int duration,
            int interval) {
        try {
            return history.getHistory(vmname, "mem", counterName, starttime, duration, interval);
        } catch (Throwable t) {
            logger.error(t);
            return new MetricResult();
        }
    }

    @Override
    public MetricResult getRealtimeCpuInfo(String vmname, String counterName, int interval) {
        try {
            return realtime.doRealTime(vmname, "cpu", counterName, interval);
        } catch (Throwable t) {
            logger.error(t);
            return new MetricResult();
        }
    }

    @Override
    public MetricResult getRealtimeDiskInfo(String vmname, String counterName, int interval) {
        try {
            return realtime.doRealTime(vmname, "disk", counterName, interval);
        } catch (Throwable t) {
            logger.error(t);
            return new MetricResult();
        }
    }

    @Override
    public MetricResult getRealtimeMemInfo(String vmname, String counterName, int interval) {
        try {
            return realtime.doRealTime(vmname, "mem", counterName, interval);
        } catch (Throwable t) {
            logger.error(t);
            return new MetricResult();
        }
    }

    @Autowired
    public void setHistory(History history) {
        this.history = history;
    }

    @Autowired
    public void setRealtime(Realtime realtime) {
        this.realtime = realtime;
    }

}
